﻿
jQuery.fn.Accordion = function () {

    var tabWP = $(this[0]);

    if ($('.ms-WPAddButton').size() == 0) {

        // get accordion configuration
        var _enabled = $(tabWP).find('.SUIEAccordionEnabled').text();
        var _firstItemOpen = $(tabWP).find('.SUIEAccordionFirstItemOpened').text();
        var _autoHeight = $(tabWP).find('.SUIEAccordionAutoHeight').text();
        var _clearStyle = $(tabWP).find('.SUIEAccordionClearStyle').text();
        var _collapsible = $(tabWP).find('.SUIEAccordionCollapsible').text();
        var _icon = $(tabWP).find('.SUIEAccordionIcon').text();
        var _event = $(tabWP).find('.SUIEAccordionEvent').text();
        var theme = $(tabWP).find('.SUIEAccordionTheme').text();

        if (_enabled == "True") {
            var zoneTable = $(tabWP).parents('.s4-wpcell-plain').parent().parent();
            if ($(zoneTable).find('.s4-wpTopTable').size() > 1) {
                var AccordionContainer = $('<div class="SUIEAccordionContainer">');
                var AccordionContainerInner = $('<div class="SUIEAccordionContainerInner">');

                $(zoneTable).parent().before(AccordionContainer);
                $(AccordionContainer).append(AccordionContainerInner);

                $(zoneTable).find('.s4-wpTopTable').each(function (i) {
                    if ($(this).find('.SUIEAccordion').size() <= 0) {
                        $(AccordionContainerInner).append('<h3><a class="SUIEAccordionLink" href="#zPanel' + i + '">' + $(this).find('.ms-WPTitle').text() + '</a></h3>');
                        var SUIEPanel = '<div id="SUIEPanel' + i + '">' + $(this).find('.ms-WPBody').html() + '<div class="SUIEClear"></div></div>';
                        $(AccordionContainerInner).append(SUIEPanel);
                    }
                });
                $(zoneTable).remove();
                $(AccordionContainer).addClass('SUIEAccordion-theme-' + theme + ' ' + theme);
                // call the accordion with properties set
                $(AccordionContainerInner).accordion({
                    active: _firstItemOpen,
                    autoHeight: _autoHeight,
                    clearStyle: _clearStyle,
                    collapsible: _collapsible,
                    event: _event,
                    icons: _icon
                });
            }
        }
    }
};

jQuery.fn.SUIEAccordionInitPreview = function () {
    try {
        $().SUIEAccordionGeneratePreview();
        // create accordion        
        $('.SUIEAccordionContainerInner').accordion();        
        // capture icon change
        $('.SUIEConfigFormField.icon .SUIEConfigFormFieldValue select').change(function (e) {
            $().SUIEAccordionGeneratePreview();
        });
        $('.SUIEConfigFormField.event .SUIEConfigFormFieldValue select').change(function (e) {
            $().SUIEAccordionGeneratePreview();
        });
        $('.SUIEConfigFormField.theme .SUIEConfigFormFieldValue select').change(function (e) {
            $().SUIEAccordionGeneratePreview();
        });
        // prev and next theme buttons
        $('.SUIEConfigFormField.theme .SUIEConfigFormFieldValue .btnThemePrevious').click(function (e) {
            e.preventDefault();
            var currentTheme = $('.SUIEConfigFormField.theme .ddlTheme').val();
            var prevTheme = "";
            $('.SUIEConfigFormField.theme .ddlTheme option').each(function (i) {
                if ($(this).val() == currentTheme && prevTheme != "") {
                    $('.SUIEConfigFormField.theme .ddlTheme').val(prevTheme)
                    $().SUIEAccordionGeneratePreview();
                    return false;
                }
                prevTheme = $(this).val();
            });
        });
        $('.SUIEConfigFormField.theme .SUIEConfigFormFieldValue .btnThemeNext').click(function (e) {
            e.preventDefault();
            var currentTheme = $('.SUIEConfigFormField.theme .ddlTheme').val();
            var applyNextTheme = false;
            $('.SUIEConfigFormField.theme .ddlTheme option').each(function (i) {
                if ($(this).val() == currentTheme) {
                    applyNextTheme = true;
                }
                else if (applyNextTheme) {
                    $('.SUIEConfigFormField.theme .ddlTheme').val($(this).val())
                    $().SUIEAccordionGeneratePreview();
                    applyNextTheme = false;
                    return false;
                }
            });
        });
    }
    catch (err) { }
};

jQuery.fn.SUIEAccordionGeneratePreview = function () {
    try {
        // clear tab preview classes 
        $('.SUIEConfigFormField.theme .ddlTheme option').each(function (i) {
            $('.SUIEAccordionContainer').removeClass('Accordion-theme-' + $(this).text());
            $('.SUIEAccordionContainer').removeClass($(this).text());
        });

        var theme = $('.SUIEConfigFormField.theme .ddlTheme').val();

        // class the tabs
        $('.SUIEAccordionContainer').addClass('SUIEAccordion-theme-' + theme + ' ' + theme); ;

        // add css    
        var cssLink = "/_layouts/SUIE/css/jquery-ui/" + theme + "/jquery-ui-1.8.16.custom.css";
        var found = false;
        if ($('#SUIEAccordionPreviewTheme').size() > 0)
            $('#SUIEAccordionPreviewTheme').attr('href', cssLink);
        else
            $("#SUIEAccordionBaseStyles").before('<link id="SUIEAccordionPreviewTheme" rel="stylesheet" type="text/css" href="' + cssLink + '" />');
    }
    catch (err) { }
}